package com.bysj.studentsystem.controller;

import com.bysj.studentsystem.entity.SysData;
import com.bysj.studentsystem.service.SysDataService;
import com.bysj.studentsystem.vo.ResponseDataVO;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (SysData)表控制层
 *
 * @author makejava
 * @since 2024-03-13 21:20:07
 */
@RestController
@RequestMapping("/data")
public class SysDataController {
    /**
     * 服务对象
     */
    @Resource
    private SysDataService sysDataService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping
    public ResponseDataVO<PageInfo<SysData>> queryByPage(@RequestParam(value = "type", required = false) String type,
                                                         @RequestParam(value = "field1", required = false) String field1,
                                                         @RequestParam(value = "field2", required = false) String field2,
                                                         @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                         @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            pageNum = 1;
            pageSize = 10;
        }
        SysData sysData = new SysData();
        sysData.setType(type);
        sysData.setField1(field1);
        sysData.setField2(field2);
        return new ResponseDataVO().ok(this.sysDataService.queryByPage(sysData,  pageNum, pageSize));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseDataVO<SysData> queryById(@PathVariable("id") Integer id) {
        return new ResponseDataVO().ok(this.sysDataService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysData 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseDataVO<SysData> add(@RequestBody SysData sysData) {
        return new ResponseDataVO().ok(this.sysDataService.insert(sysData));
    }

    /**
     * 编辑数据
     *
     * @param sysData 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseDataVO<SysData> edit(@RequestBody SysData sysData) {
        return new ResponseDataVO().ok(this.sysDataService.update(sysData));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseDataVO<Boolean> deleteById(@RequestParam("id") Integer id) {
        return new ResponseDataVO().ok(this.sysDataService.deleteById(id));
    }

}

